home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 019a / view002.zip / VIEWSCR.C < prev   
C/C++ Source or Header  |  1991-09-14  |  7KB  |  374 lines

  1. /*
  2. View 0.02 - A simple,small,windowed, text file viewer.
  3.  
  4. Copyright (c) 1991 James P. Goodwin.
  5. All rights reserved.
  6.  
  7. Redistribution and use in source and binary forms are per-
  8. mitted provided that the above copyright notice is dupli-
  9. cated in all such forms and that any documentation,
  10. advertising materials, and other materials related to such
  11. distribution and use acknowledge that the software was
  12. developed by James P. Goodwin.
  13.  
  14. THE SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS
  15. OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE
  16. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PAR-
  17. TICULAR PURPOSE.
  18. */
  19.  
  20.  
  21. #include <stdio.h>
  22. #include <fcntl.h>
  23. #include <sys\types.h>
  24. #include <sys\stat.h>
  25. #include <io.h>
  26. #include <share.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <dos.h>
  30. #include <share.h>
  31. #include <conio.h>
  32. #include <limits.h>
  33. #include <direct.h>
  34. #include "view.h"
  35.  
  36. /*
  37.   View screen and input routines
  38. */
  39.  
  40. int view_getvidmode( void )
  41. {
  42.   _asm
  43.       {
  44.       mov ax,0f00h
  45.       int 10h
  46.       xor ah,ah
  47.       }
  48. }
  49.  
  50. void view_goto(int row,int col)
  51. {
  52.   view_offset = (row*(view_cols*2))+(col*2);
  53. }
  54.  
  55. void view_putc( int ochar )
  56. {
  57.   int offset = view_offset;
  58.  
  59.   screen[offset] = ochar;
  60.   offset++;
  61.   screen[offset] = view_attr;
  62.   offset++;
  63.  
  64.   view_offset = offset;
  65. }
  66.  
  67. void view_fill(int fill, int count)
  68. {
  69.   while(count > 0) 
  70.     { 
  71.     view_putc(fill);
  72.     count --; 
  73.     }
  74. }
  75.  
  76. void view_puts(UCHAR *str, int len)
  77. {
  78.   while( *str && len )
  79.     {
  80.     view_putc(*str);
  81.     str ++;
  82.     len --;
  83.     }
  84.  
  85.   while( len )
  86.     {
  87.     view_putc(' ');
  88.     len --;
  89.     }
  90. }
  91.  
  92. int view_prompt( UCHAR *title, UCHAR *prompt, UCHAR *retbuf, int retlen )
  93. {
  94.    VIEWSAVE *vs;
  95.    int row,col,width;
  96.    int ret;
  97.  
  98.    row = (view_rows/2);
  99.    width = strlen(prompt)+1+retlen+2;
  100.    col = (view_cols/2) - width/2;
  101.  
  102.    vs = view_getsave(row-1,col-1,3,width+2);
  103.  
  104.    view_attr = GET_STRING;
  105.    view_frame(title,row-1,col-1,3,width+2);
  106.    view_goto(row,col);
  107.    view_puts(prompt,width);
  108.  
  109.    ret = view_gets(row,col+1+strlen(prompt),retlen,retbuf);
  110.  
  111.    view_putsave(vs);
  112.  
  113.    return(ret);
  114. }
  115.  
  116.  
  117.  
  118. int view_gets( int row,int col, int len, UCHAR *str)
  119. {
  120.    int  offset;
  121.    char scan;
  122.  
  123.    view_attr = GET_STRING;
  124.  
  125.    offset = 0;
  126.  
  127.    for(;;)
  128.      {
  129.      view_goto(row,col);
  130.      view_puts(str,len+1);
  131.      view_goto(row,col+offset);
  132.      view_attr = GET_STRING_CURSOR;
  133.      view_puts(str+offset,1);
  134.      view_attr = GET_STRING;
  135.  
  136.      scan = getch();
  137.  
  138.      switch( scan )
  139.        {
  140.        case BACKSPACE:
  141.          if (offset > 0)
  142.            {
  143.            offset --;
  144.            str[offset] = '\0';
  145.            }
  146.        break;
  147.  
  148.        case RETURN:
  149.          str[offset] = '\0';
  150.          return(TRUE);
  151.        break;
  152.  
  153.        case ESC:
  154.          return(FALSE);
  155.        break;
  156.  
  157.        case 0:
  158.           scan = getch();
  159.        break;
  160.                                 
  161.        default:
  162.          if (scan > 31 && scan < 127)
  163.            {
  164.            if (offset < len)
  165.              {
  166.              str[offset] = scan;
  167.              offset ++;
  168.              str[offset] = '\0';
  169.              }
  170.            }
  171.        break;
  172.        }
  173.      }
  174. }
  175.  
  176. void view_frame( UCHAR *title,int row,int col,int rows,int cols )
  177. {
  178.   int r;
  179.   int off;
  180.   int mod;
  181.   int wid;
  182.   int offset;
  183.  
  184.   wid = cols-2;
  185.  
  186.   view_goto(row,col);
  187.   view_putc('┌');
  188.   view_fill('─',wid);
  189.   view_putc('┐');
  190.   view_goto(row,col+1);
  191.   view_puts(title,min(wid,strlen(title)));
  192.  
  193.   view_goto(row+1,col);
  194.  
  195.   off = ((wid)*2);
  196.   mod = (view_cols*2)-(off+4);
  197.  
  198.   offset = view_offset;
  199.  
  200.   for (r = 1; r < rows-1; r ++)
  201.     {
  202.     screen[offset]  = '│';
  203.     offset ++;
  204.     screen[offset]  = view_attr;
  205.     offset ++;
  206.     offset += off;
  207.     screen[offset]  = '│';
  208.     offset ++;
  209.     screen[offset]  = view_attr;
  210.     offset ++;
  211.     offset += mod;
  212.     }
  213.  
  214.   view_offset = offset;
  215.  
  216.   view_putc('└');
  217.   view_fill('─',wid);
  218.   view_putc('┘');
  219. }
  220.  
  221. void view_putsave( VIEWSAVE *save )
  222. {
  223.    view_unpackbuf(save->buf,save->row,save->col,save->rows,save->cols);
  224.  
  225.    free(save->buf);
  226.    free(save);
  227. }  
  228.  
  229. VIEWSAVE *view_getsave( int row,int col,int rows,int cols )
  230. {
  231.    UCHAR *buf;
  232.    VIEWSAVE *getsave;
  233.    int offset;
  234.  
  235.    getsave = malloc(sizeof(VIEWSAVE));
  236.    if (!getsave) view_error(1,"view_getsave: alloc getsave");
  237.  
  238.    getsave->row  = row;
  239.    getsave->col  = col;
  240.    getsave->rows = rows;
  241.    getsave->cols = cols;
  242.  
  243.    getsave->buf = malloc((rows*(cols*2)));
  244.    if (!getsave->buf) view_error(1,"view_getsave: alloc buf");
  245.  
  246.    buf = getsave->buf;
  247.    cols *= 2;
  248.  
  249.    view_goto(getsave->row,getsave->col);
  250.  
  251.    offset = view_offset;
  252.  
  253.    for (row = 0; row < rows; row ++)
  254.      {
  255.      for (col = 0; col < cols; col ++)
  256.        {
  257.        *buf = screen[offset];
  258.        offset ++;
  259.        buf ++;
  260.        }
  261.      offset += ((view_cols*2)-cols);
  262.      }
  263.  
  264.    view_offset = offset;
  265.  
  266.    getsave->buf = realloc(getsave->buf,view_packbuf(getsave->buf,(rows*cols)));
  267.  
  268.    return(getsave);
  269. }
  270.  
  271.  
  272. int view_packbuf( UCHAR *buf, int size )
  273. {
  274.    UCHAR *from, *to;
  275.    UCHAR count;
  276.    UCHAR idx;
  277.  
  278.    if (!*buf) *buf = ' ';
  279.  
  280.    for (count = 0,to = buf,from = buf+2; from-buf < size; from += 2)
  281.      {
  282.      if (!*from) *from = ' ';
  283.  
  284.      if (((*from == *(from-2)) && (*(from+1) == *(from-1))) && (count < 255))
  285.        count ++;
  286.      else if (count > 4)
  287.        {
  288.        *(to++) = '\0';
  289.        *(to++) = count;
  290.        *(to++) = *(from-2);
  291.        *(to++) = *(from-1);
  292.        count = 0;
  293.        }
  294.      else
  295.        {
  296.        for (idx = 0; idx <= count; idx ++)
  297.          {
  298.          *(to++) = *(from-2);
  299.          *(to++) = *(from-1);
  300.          }
  301.        count = 0;
  302.        }
  303.      }
  304.  
  305.    if (count > 4)
  306.      {
  307.      *(to++) = '\0';
  308.      *(to++) = count;
  309.      *(to++) = *(from-2);
  310.      *(to++) = *(from-1);
  311.      count = 0;
  312.      }
  313.    else
  314.      {
  315.      for (idx = 0; idx <= count; idx ++)
  316.        {
  317.        *(to++) = *(from-2);
  318.        *(to++) = *(from-1);
  319.        }
  320.      count = 0;
  321.      }
  322.  
  323.    return(to-buf);
  324. }
  325.  
  326. void view_unpackbuf( UCHAR *buf, int row, int col, int rows, int cols )
  327. {
  328.    UCHAR count,idx;
  329.    UCHAR chr,atr;
  330.    int   ccol;
  331.  
  332.    view_goto(row,col);
  333.  
  334.    rows = row+rows;
  335.    cols = col+cols;
  336.    ccol = col;
  337.  
  338.    while( row < rows )
  339.      {
  340.      if (!*buf)
  341.        {
  342.        count = *(buf+1);
  343.        chr   = *(buf+2);
  344.        atr   = *(buf+3);
  345.        buf   += 4;
  346.        }
  347.      else
  348.        {
  349.        count = 0;
  350.        chr   = *(buf);
  351.        atr   = *(buf+1);
  352.        buf   += 2;
  353.        }
  354.  
  355.      for (idx = 0; idx <= count; idx ++)
  356.        {
  357.        screen[view_offset] = chr;
  358.        view_offset ++;
  359.        screen[view_offset] = atr;
  360.        view_offset ++;
  361.        ccol ++;
  362.        if (ccol >= cols)
  363.          {
  364.          ccol = col;
  365.          row  ++;
  366.          view_goto(row,ccol);
  367.          }
  368.        }
  369.      }
  370. }
  371.  
  372.        
  373.  
  374.